﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Sauces.Bussinees.Entities.Caja;
using Sauces.Repository.Caja.Service;

namespace Sauces.Repository.Caja
{
    public class ClienteRepository:MasterRepository,IClienteRepository
    {

        public List<Cliente> getAllCliente()
        {
            var query = from c in Context.Cliente
                            select c;

            return query.ToList();
        }


        public Cliente getFromClienteById(int id)
        {
            return Context.Cliente.Find(id);
            
        }

        public void addCliente(Cliente cliente)
        {
            Context.Cliente.Add(cliente);
            Context.SaveChanges();
        }

        public void upDateCliente(Cliente cliente)
        {
            Context.Entry(cliente).State=System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void removeCliente(int id)
        {
            var existe = Context.Cliente.Where(c=>c.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                Context.Cliente.Remove(existe);
                Context.SaveChanges();
            }
        }


        public List<Cliente> getAllClienteByDniRuc(string criterio)
        {
           var query = from c in Context.Cliente
                           select c;
           if (criterio != null && criterio != "")
           {
               query = from c in query
                       where c.Dni.ToUpper().Contains(criterio.ToUpper()) ||
                             c.Ruc.ToUpper().Contains(criterio.ToUpper())
                       select c;
           }
           return query.ToList();
        }


        public Cliente getFromClienteByDni(string dni)
        {
           return  Context.Cliente.Where(c => c.Dni.Equals(dni)).SingleOrDefault();
        }

        public Cliente getFromClienteByRuc(string Ruc)
        {
            return Context.Cliente.Where(c => c.Ruc.Equals(Ruc)).SingleOrDefault();
        }
    }
}
